package lyq.spec.edu.week14_home01;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ChangingPasswords extends AppCompatActivity {

    private EditText mEtStudentNumber; // 帐号编辑框
    private EditText mEtPwd; // 密码编辑框
    private EditText mCfPwd; // 密码编辑框


    private String Result;
    private static final String TAG = "ChangingPasswords";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changing_passwords);
        mEtStudentNumber = (EditText) findViewById(R.id.change_student_number);
        mEtPwd = (EditText) findViewById(R.id.change_psw);
        mCfPwd = (EditText) findViewById(R.id.change_psw2);


    }

    public void ClickChange(View v) {
        sendRequestWihtOkhttp();

        if(Result == "true" && mEtPwd.getText().toString() == mCfPwd.getText().toString())
        {
            Toast.makeText(ChangingPasswords.this,"修改成功",Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(ChangingPasswords.this,LoginActivity.class);
            startActivity(intent);
        }
        if(Result == "false")
        {
            Toast.makeText(ChangingPasswords.this,"修改失败",Toast.LENGTH_SHORT).show();
            mEtStudentNumber.setText(null);
            mEtPwd.setText(null);
            mCfPwd.setText(null);
        }
        if(mEtPwd.getText().toString() != mCfPwd.getText().toString()) {
            Toast.makeText(ChangingPasswords.this, "前后密码不一致", Toast.LENGTH_SHORT).show();
        }


    }

    /**
     * 发起网络请求
     */
    private void sendRequestWihtOkhttp(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 构造客户端，相当于浏览器
                    OkHttpClient client = new OkHttpClient();
                    // 构造请求，相当于在浏览器里输入地址
                    Request request = new Request
                            .Builder()
                            .url("http://www.yyjsx.xyz/api/student/updatePassword"+"?studentNumber="+mEtStudentNumber.getText().toString()+"&password="+mEtPwd.getText().toString())
                            .build();
                    // 发送请求，相当于在浏览器按回车
                    Response response = client.newCall(request).execute();
                    // 更新ListView
                    String body = response.body().string();
//                    Log.d(TAG, body);
                    parseJson(body);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void parseJson(String jsonStr){
        try {
            JSONObject jobj = new JSONObject(jsonStr);
            Result = jobj.getString("success");

            Log.d(TAG,Result);


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}

